//
//  Single+Cache.swift
//  XNetwork_Example
//
//  Created by x on 2024/8/9.
//  Copyright © 2024 CocoaPods. All rights reserved.
//

import Moya
import RxSwift


extension PrimitiveSequence where Trait == SingleTrait, Element == Moya.Response {
    // 缓存response
    public func storeCachedResponse<Target>(for target: Target)
    -> Single<Element> where Target: TargetType, Target: Cacheable, Target.ResponseType == Element {
        return map { response -> Element in
            if target.allowsStorage(response) {
                try? target.storeCachedResponse(response)
            }
            
            return response
        }
    }
}
